<!doctype html>
<html lang="zh-cn">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>线程安全的类 | lsy&#39;blog</title>
    <meta property="og:title" content="线程安全的类 - lsy&#39;blog">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2020-04-21T11:40:20&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2020-04-21T11:40:20&#43;08:00'>
        
    <meta name="Keywords" content="golang,go语言,go语言笔记,lsy,java,博客,软件架构">
    <meta name="description" content="线程安全的类">
        
    <meta name="author" content="lsy">
    <meta property="og:url" content="http://kklt1996.gitee.io/blog/post/java/2020-04-21-juc-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E7%B1%BB/">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href='/blog/css/normalize.css'>
    <link rel="stylesheet" href='/blog/css/style.css'>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    


    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>


<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="http://kklt1996.gitee.io/blog/">
                        lsy&#39;blog
                    </a>
                
                <p class="description">专注于Java、Go语言(golang)、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="http://kklt1996.gitee.io/blog/">首页</a>
                    
                    <a  href="http://kklt1996.gitee.io/blog/archives/" title="归档">归档</a>
                    
                    <a  href="http://kklt1996.gitee.io/blog/categories/" title="分类">分类</a>
                    
                    <a  href="http://kklt1996.gitee.io/blog/tags/" title="标签">标签</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#定义">定义</a></li>
    <li><a href="#atomicinteger">AtomicInteger</a></li>
    <li><a href="#atomiclong">AtomicLong</a></li>
    <li><a href="#longadder">LongAdder</a></li>
    <li><a href="#atomicboolean">AtomicBoolean</a></li>
    <li><a href="#atomicreference">AtomicReference</a></li>
    <li><a href="#atomicstampedreference">AtomicStampedReference</a></li>
    <li><a href="#atomiclongarray">AtomicLongArray</a></li>
    <li><a href="#其他更多线程安全的类">其他更多线程安全的类</a></li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">线程安全的类</h1>
        </header>
        <date class="post-meta meta-date">
            2020年4月21日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='http://kklt1996.gitee.io/categories/java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B'>java并发编程</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="clear" style="display: none">
            <div class="toc-article">
                <div class="toc-title">文章目录</div>
            </div>
        </div>
        
        <div class="post-content">
            <p>线程安全的类概念?如何使用?</p>
<h2 id="定义">定义</h2>
<ol>
<li>
<p>线程安全的类
当多个线程访问某个类时,不管运行时环境采用何种调度方式,或者这些进程将如何交替执行,
并且在主调代码中不需要额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程安全的.
简单的说就是多线程操作某个共享变量在代用代码中不加各种形式锁的前提下就能得到我们预期的结果,不会有线程安全问题.</p>
</li>
<li>
<p>线程安全性</p>
</li>
</ol>
<ul>
<li>原子性: 提供了互斥的访问,同时只有一个线程进行操作.</li>
<li>可见性: 一个线程对主内存的修改,可以及时的被其他线程观察到.
一个线程修改主内存的值后,其他线程在用到这个变量的时候无论本地内存是否有变量的副本,都会从主内存中重新加载
变量的最新值.</li>
<li>有序性: 一个线程观察其他线程中的指令执行顺序,由于指令重排序的存在,该观察结果一般杂乱无序.</li>
</ul>
<h2 id="atomicinteger">AtomicInteger</h2>
<p>java.util.concurrent.atomic包下的AtomicInteger类是通过cas来实现的线程安全的类. cas的全称是compareAndSwap.
以AtomicInteger的getAndAdd()方法为例.最终会调用Unsafe类的getAndAddInt()方法.</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">public</span> <span style="color:#000;font-weight:bold">class</span> <span style="color:#458;font-weight:bold">AtomicInteger</span> <span style="color:#000;font-weight:bold">extends</span> Number <span style="color:#000;font-weight:bold">implements</span> java<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">io</span><span style="color:#000;font-weight:bold">.</span><span style="color:#008080">Serializable</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>     <span style="color:#998;font-style:italic">/**
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">     * Atomically adds the given value to the current value.
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">     *
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">     * @param delta the value to add
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">     * @return the previous value
</span></span></span><span style="display:flex;"><span><span style="color:#998;font-style:italic">     */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">public</span> <span style="color:#000;font-weight:bold">final</span> <span style="color:#458;font-weight:bold">int</span> <span style="color:#900;font-weight:bold">getAndAdd</span><span style="color:#000;font-weight:bold">(</span><span style="color:#458;font-weight:bold">int</span> delta<span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">return</span> unsafe<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">getAndAddInt</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">this</span><span style="color:#000;font-weight:bold">,</span> valueOffset<span style="color:#000;font-weight:bold">,</span> delta<span style="color:#000;font-weight:bold">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">}</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>查看Unsafe类的getAndAddInt()方法.</p>
<ul>
<li>var1是AtomicInteger类的对象.</li>
<li>var2是要增加值的变量的原始long类型的值.</li>
<li>var4是要增加的值.</li>
<li>var5是通过var1和var2从堆内存中取出来的变量的值.</li>
</ul>
<p>通过native方法来比较线程持有的变量副本和从堆内存中取出的变量的值是否相同,如果相同就更新堆内存中的值返回true;
如果不相同就将堆内存中的变量值赋值给线程内的副本返回false.循环调用此方法,直到返回true为止.</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">public</span> <span style="color:#000;font-weight:bold">final</span> <span style="color:#000;font-weight:bold">class</span> <span style="color:#458;font-weight:bold">Unsafe</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">public</span> <span style="color:#000;font-weight:bold">final</span> <span style="color:#458;font-weight:bold">int</span> <span style="color:#900;font-weight:bold">getAndAddInt</span><span style="color:#000;font-weight:bold">(</span>Object var1<span style="color:#000;font-weight:bold">,</span> <span style="color:#458;font-weight:bold">long</span> var2<span style="color:#000;font-weight:bold">,</span> <span style="color:#458;font-weight:bold">int</span> var4<span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>        <span style="color:#458;font-weight:bold">int</span> var5<span style="color:#000;font-weight:bold">;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">do</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>            var5 <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">this</span><span style="color:#000;font-weight:bold">.</span><span style="color:#008080">getIntVolatile</span><span style="color:#000;font-weight:bold">(</span>var1<span style="color:#000;font-weight:bold">,</span> var2<span style="color:#000;font-weight:bold">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">}</span> <span style="color:#000;font-weight:bold">while</span><span style="color:#000;font-weight:bold">(!</span><span style="color:#000;font-weight:bold">this</span><span style="color:#000;font-weight:bold">.</span><span style="color:#008080">compareAndSwapInt</span><span style="color:#000;font-weight:bold">(</span>var1<span style="color:#000;font-weight:bold">,</span> var2<span style="color:#000;font-weight:bold">,</span> var5<span style="color:#000;font-weight:bold">,</span> var5 <span style="color:#000;font-weight:bold">+</span> var4<span style="color:#000;font-weight:bold">));</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">return</span> var5<span style="color:#000;font-weight:bold">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">}</span>
</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>使用AtomicInteger来实现全的计数操作</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>    <span style="color:#3c5d5d;font-weight:bold">@Test</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">public</span> <span style="color:#458;font-weight:bold">void</span> <span style="color:#900;font-weight:bold">atomicIntegerTest</span><span style="color:#000;font-weight:bold">(){</span>
</span></span><span style="display:flex;"><span>        AtomicInteger atomicInteger <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">new</span> AtomicInteger<span style="color:#000;font-weight:bold">(</span><span style="color:#099">0</span><span style="color:#000;font-weight:bold">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#458;font-weight:bold">int</span> countTime <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">100</span><span style="color:#000;font-weight:bold">;</span>
</span></span><span style="display:flex;"><span>        CountDownLatch countDownLatch <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">new</span> CountDownLatch<span style="color:#000;font-weight:bold">(</span>countTime<span style="color:#000;font-weight:bold">);</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#458;font-weight:bold">int</span> i <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">0</span><span style="color:#000;font-weight:bold">;</span> i <span style="color:#000;font-weight:bold">&lt;</span> countTime<span style="color:#000;font-weight:bold">;</span> i<span style="color:#000;font-weight:bold">++)</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>            executorService<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">execute</span><span style="color:#000;font-weight:bold">(()-&gt;{</span>
</span></span><span style="display:flex;"><span>                atomicInteger<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">incrementAndGet</span><span style="color:#000;font-weight:bold">();</span>
</span></span><span style="display:flex;"><span>                countDownLatch<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">countDown</span><span style="color:#000;font-weight:bold">();</span>
</span></span><span style="display:flex;"><span>            <span style="color:#000;font-weight:bold">});</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">}</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">try</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>            countDownLatch<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">await</span><span style="color:#000;font-weight:bold">();</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">}</span> <span style="color:#000;font-weight:bold">catch</span> <span style="color:#000;font-weight:bold">(</span>InterruptedException e<span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>            e<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">printStackTrace</span><span style="color:#000;font-weight:bold">();</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">}</span>
</span></span><span style="display:flex;"><span>        System<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">out</span><span style="color:#000;font-weight:bold">.</span><span style="color:#008080">println</span><span style="color:#000;font-weight:bold">(</span><span style="color:#d14">&#34;count:&#34;</span><span style="color:#000;font-weight:bold">+</span>atomicInteger<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">get</span><span style="color:#000;font-weight:bold">());</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>输出</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#900;font-weight:bold">count:</span><span style="color:#099">100</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="atomiclong">AtomicLong</h2>
<p>AtomicLong也是是采用cas增加的方法来实现的.因为采用循环尝试机制去更新,在线程竞争激烈的情况下可能会多次循环尝试,更新成功率较低.
jvm允许将64位long,double的读操作或者写操作拆成2个32位的操作.提高其并行度.</p>
<h2 id="longadder">LongAdder</h2>
<p>jdk8中LongAdder的实现方式对更新较为激烈的的情况之下做了优化:热点数据分离.
把long值分离为数组,每个线程访问时通过hash等算法映射到数组的一项中计数,最终的计数结果是数组的求和累加.
热点数据value就被分离成多个单元,提高了并行度.高并发分散竞争提高性能,但准确度会有偏差,适用于粗粒度的统计数据,要求准确度用AtomicLong.</p>
<h2 id="atomicboolean">AtomicBoolean</h2>
<p>AtomicBoolean常用于控制某些逻辑被执行一次.
并发执行10次set操作.</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>    <span style="color:#3c5d5d;font-weight:bold">@Test</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">public</span> <span style="color:#458;font-weight:bold">void</span> <span style="color:#900;font-weight:bold">atomicBooleanTest</span><span style="color:#000;font-weight:bold">(){</span>
</span></span><span style="display:flex;"><span>        AtomicBoolean atomicBoolean <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">new</span> AtomicBoolean<span style="color:#000;font-weight:bold">();</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#458;font-weight:bold">int</span> i <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">0</span><span style="color:#000;font-weight:bold">;</span> i <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#099">10</span><span style="color:#000;font-weight:bold">;</span> i<span style="color:#000;font-weight:bold">++)</span> <span style="color:#000;font-weight:bold">{</span>
</span></span><span style="display:flex;"><span>            executorService<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">execute</span><span style="color:#000;font-weight:bold">(()-&gt;{</span>
</span></span><span style="display:flex;"><span>                <span style="color:#458;font-weight:bold">boolean</span> setFlag <span style="color:#000;font-weight:bold">=</span> atomicBoolean<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">compareAndSet</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">false</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">true</span><span style="color:#000;font-weight:bold">);</span>
</span></span><span style="display:flex;"><span>                System<span style="color:#000;font-weight:bold">.</span><span style="color:#008080">out</span><span style="color:#000;font-weight:bold">.</span><span style="color:#008080">println</span><span style="color:#000;font-weight:bold">(</span>setFlag<span style="color:#000;font-weight:bold">);</span>
</span></span><span style="display:flex;"><span>            <span style="color:#000;font-weight:bold">});</span>
</span></span><span style="display:flex;"><span>        <span style="color:#000;font-weight:bold">}</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000;font-weight:bold">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>输出结果</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>true
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span><span style="display:flex;"><span>false
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="atomicreference">AtomicReference</h2>
<p>AtomicReference用于更新引用对象的值.需要注意的是cas比较的是引用的地址,并且不是自旋操作,比较一次失败之后就不在尝试.</p>
<h2 id="atomicstampedreference">AtomicStampedReference</h2>
<p>AtomicStampReference类相对于AtomicReference是解决cas的ABA问题.
ABA问题描述: 当前线程读取值变量值为A,其他线程修改其值为B,然后其他线程又修改为A.则当前线程对变量值进行更新时会认为此变量没有被更新过.
AtomicStampReference类通过给变量引入递增的版本的概念避免ABA问题.类似于乐观锁的实现原理.</p>
<h2 id="atomiclongarray">AtomicLongArray</h2>
<p>AtomicLongArray类中,数组的每一个位置的元素更新都是原子操作.</p>
<h2 id="其他更多线程安全的类">其他更多线程安全的类</h2>
<p>AtomicReferenceFieldUpdater原子更新引用对象的某个引用类型属性的值
AtomicIntegerFieldUpdater原子更新引用对象的某个Integer类型属性的值
<img src="" alt="原子操作类"></p>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="http://kklt1996.gitee.io/blog/">lsy</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="http://kklt1996.gitee.io/blog/post/java/2020-04-21-juc-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E7%B1%BB/">http://kklt1996.gitee.io/blog/post/java/2020-04-21-juc-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E7%B1%BB/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/blog/post/java/2020-04-21-java%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B/">java内存模型</a></li>
        
        <li><a href="/blog/post/mybatis/2020-04-12-mybatis%E6%89%B9%E9%87%8F%E6%8F%92%E5%85%A5%E8%BF%94%E5%9B%9E%E8%87%AA%E5%A2%9E%E4%B8%BB%E9%94%AE/">mybatis批量插入返回自增主键</a></li>
        
        <li><a href="/blog/post/java/2020-04-14-juc-%E7%BA%BF%E7%A8%8B%E6%B1%A0/">java中的线程池</a></li>
        
        <li><a href="/blog/post/java/2020-04-14-juc-%E4%BF%A1%E5%8F%B7%E9%87%8F/">Semaphore信号量</a></li>
        
        <li><a href="/blog/post/mysql/2020-04-06-%E4%BD%BF%E7%94%A8explain%E5%88%86%E6%9E%90sql%E8%AF%AD%E5%8F%A5/">使用explain分析sql语句</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            没有标签
            
        </div>
    </article>
    
    

    
    
</div>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='http://kklt1996.gitee.io/blog/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="http://kklt1996.gitee.io/blog/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E7%94%9F%E4%BA%A7%E6%B6%88%E8%B4%B9%E6%A8%A1%E5%9E%8B/" title="golang中的生产消费模型">golang中的生产消费模型</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/golang/2023-10-31-go%E5%8D%8F%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/" title="golang协程间通信">golang协程间通信</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-05-26-%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/" title="单利模式发布对象">单利模式发布对象</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/redis/2020-05-20-redis%E5%AE%9E%E7%8E%B0%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/" title="Redis实现分布式锁">Redis实现分布式锁</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-05-04-%E5%8E%9F%E5%AD%90%E6%80%A7-%E5%8F%AF%E8%A7%81%E6%80%A7-%E6%9C%89%E5%BA%8F%E6%80%A7/" title="原子性 可见性 有序性">原子性 可见性 有序性</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-21-juc-%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E7%B1%BB/" title="线程安全的类">线程安全的类</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-21-java%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B/" title="java内存模型">java内存模型</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/mybatis/2020-04-12-mybatis%E6%89%B9%E9%87%8F%E6%8F%92%E5%85%A5%E8%BF%94%E5%9B%9E%E8%87%AA%E5%A2%9E%E4%B8%BB%E9%94%AE/" title="mybatis批量插入返回自增主键">mybatis批量插入返回自增主键</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-14-juc-%E7%BA%BF%E7%A8%8B%E6%B1%A0/" title="java中的线程池">java中的线程池</a>
    </li>
    
    <li>
        <a href="http://kklt1996.gitee.io/blog/post/java/2020-04-14-juc-%E4%BF%A1%E5%8F%B7%E9%87%8F/" title="Semaphore信号量">Semaphore信号量</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title">分类</h3>
<ul class="widget-list">
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/elasticsearch/">elasticsearch (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/golang/">golang (2)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/">java并发编程 (6)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/json/">json (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/mybatis/">mybatis (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/mysql/">mysql (3)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/redis/">redis (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/springBoot/">springBoot (11)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/ubuntu/">ubuntu (3)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/">数据结构和算法 (1)</a></li>
    
    <li><a href="http://kklt1996.gitee.io/blog/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title">标签</h3>
<div class="tagcloud">
    
    <a href="http://kklt1996.gitee.io/blog/tags/aspect/">aspect</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/devTool/">devTool</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/docker/">docker</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/filter/">filter</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/hibernate/">hibernate</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/idea/">idea</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/intercept/">intercept</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/interceptor/">interceptor</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/jackjson/">jackjson</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/resultful/">resultful</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/springTest/">springTest</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/spring%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/">spring自定义异常处理</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/swagger/">swagger</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/swap/">swap</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/validator/">validator</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/%E5%8A%A8%E6%80%81%E6%95%B0%E6%8D%AE%E6%BA%90/">动态数据源</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a>
    
    <a href="http://kklt1996.gitee.io/blog/tags/%E9%98%B2%E7%81%AB%E5%A2%99/">防火墙</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://gitee.com/kklt1996" title="码云地址">码云地址</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="http://kklt1996.gitee.io/blog/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>

    

    <footer id="footer">
    <div class="container">
        &copy; 2023 <a href="http://kklt1996.gitee.io/blog/">lsy&#39;blog By lsy</a>.
        Powered by <a rel="nofollow noreferer noopener" href="https://gohugo.io" target="_blank">Hugo</a>.
        <a href="https://www.flysnow.org/" target="_blank">Theme</a> based on <a href="https://github.com/flysnow-org/maupassant-hugo" target="_blank">maupassant</a>.
        
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>


<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/blog/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>

</body>

</html>